package com.lmk.t02;

public class Station {
    Integer tickets;

    public Station(Integer tickets) {
        this.tickets = tickets;
    }
    public  boolean  sale(String person){
        if(tickets>0){
          synchronized (this) {
              //使用同步代码块  注意双重检查
              if(tickets<=0)
                  return false;
              System.out.println(person+"购到："+tickets);
              tickets--;
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              return true;
          }
            }

        else {
            System.out.println("票已售罄");
        }
        return false;

    }
}
